import Vue from 'vue'
import VueRouter from 'vue-router'


const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)


const ModuleFn = require.context('./routes', false, /\.js$/i);
const routes = ModuleFn.keys().reduce((prev, curr) => {
  let route = ModuleFn(curr).default;
  if (Array.isArray(route)) {
    prev.push(...route);
  } else {
    prev.push(route);
  }
  return prev;
}, []);

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
